<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/11/30 0030
 * Time: 17:08
 */

namespace app\index\controller;
use app\index\model\Order;
use app\index\model\Browserecord;
use app\index\model\Cases;
use app\index\model\Drawing;
use app\index\model\Information;
use app\index\model\InformationKeyword;
use app\index\model\User;
use app\index\model\UserCookie;
use \think\Config;

class Design extends Base
{
    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:图纸列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function designLists($key = "")
    {
        $keyword = input("keyword/s");
        $structure = input("structure/d");//结构Id
        $house = input("house/d");//户型结构ID
        $order = input("order/d");//排序ID
        $p = input("p/d");//分页参数
        $houseName = '';//户型结构名称
        $structureName ='';//结构名称
        $layct = '';//楼层id
        $layctPY = '';//楼层拼音
        $layctName = '';//楼层名称
        $style_id = '';//风格id
        $stylePY = '';//风格拼音
        $styleName = '';//风格名称
        $orders = '';//人气，面积，价格，最新，排序
        $baseUrl = request()->baseUrl();
        $paraUrl = $this->paraUrl($structure,$house);
        //人气，面积，价格，最新，排序参数后缀URL,start
        $orderParaUrl = (substr(request()->url(),strlen(request()->baseUrl())));
        if(!empty($p)){
            //判断是直接分页还是搜索后再分页
            $domain = strstr($orderParaUrl, '&');
            if($domain != false){
                $orderParaUrl = substr($orderParaUrl,0,strpos($orderParaUrl,'&p'));
            }else{
                $orderParaUrl = substr($orderParaUrl,0,strpos($orderParaUrl,'p')-1);
            }
        }
        //人气，面积，价格，最新，排序参数后缀URL,end

        $Drawing = new Drawing();
        $config = new \app\index\model\Config();
        $informationKeyword = new InformationKeyword();
        $cases = new Cases();
        $information = new Information();
        //别墅层数
        $drawingfloor = config("numberBuildings");
        foreach ($drawingfloor as $k=>$val){
            //获取后缀参数URL
            if(!empty($structure)){
                if(!empty($house)){
                    $drawingfloor[$k]['config_url'] = "?structure=".$structure."&house=".$house;
                }else{
                    $drawingfloor[$k]['config_url'] = "?structure=".$structure;
                }
            }else{
                if($house){
                    $drawingfloor[$k]['config_url'] = "?house=".$house;
                }else{
                    $drawingfloor[$k]['config_url'] = "";
                }
            }
        }

        //别墅风格
        $drawingStyle = $config->getConfigTypeListPY("DrawingStyle");
        foreach ($drawingStyle as $k=>$val){
            //获取后缀参数URL
            if(!empty($structure)){
                if(!empty($house)){
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure."&house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "?structure=".$structure;
                }
            }else{
                if($house){
                    $drawingStyle[$k]['config_url'] = "?house=".$house;
                }else{
                    $drawingStyle[$k]['config_url'] = "";
                }
            }
        }

        if(!empty($key)){
            $is = true;
            //找楼层
            foreach ($drawingfloor as $val){
                $str = stristr($key,$val["screen_one"]);
                if($str !== false){
                    $is = false;
                    //层数搜索存在
                    if($key==$val["screen_one"]){
                        //单个搜索
                        $layct = $val['config_id'];
                        $layctPY = $val["screen_one"];
                        $layctName = $val["config_name"];
                        break;
                    }else{
                        //楼层和风格联合搜索
                        $layct = $val['config_id'];
                        $layctPY = $val["screen_one"];
                        $layctName = $val["config_name"];
                        $style = str_replace($val["screen_one"],"",$str);
                        foreach ($drawingStyle as $v){
                            if($v['py']==$style){
                                $style_id = $v['config_id'];
                                $stylePY = $style;
                                $styleName = $v['config_name'];
                                break;
                            }
                        }
                    }
                }
            }
            //找不到楼层
            if($is){
                foreach ($drawingStyle as $sk=>$SVal) {//遍历风格
                    if ($SVal["py"] == $key) {
                        $style_id = $SVal['config_id'];
                        $stylePY = $key;
                        $styleName = $SVal['config_name'];
                        break;
                    }
                }

            }
        }

        //人气，面积，价格，最新，排序
        if(!empty($order)){
            $orderInfo = config("drawingOrder");
            foreach ($orderInfo as $val){
                if($order == $val["order_id"]){
                    $orders["type"] = $val["type"];
                    $orders["type_order"] = $val["type_order"];
                }

            }
        }
        //图纸列表
        $drawingList = $Drawing->drawingList($keyword,$style_id,$layct,$structure,$house,$is_top="0",$orders);
        $drawingListCount = $Drawing->drawingListCount($keyword,$style_id,$layct,$structure,$house,$is_top="0",$orders);
        $config_info = $config->drawingList();//配置参数的ID

        //结构形式
        $structureStyle = $config->configType("DrawingFrameStructure");
        foreach ($structureStyle as $key=>$val){
            if($val["config_id"] == $structure){
                $structureName = $val['config_name'];
            }
            if(!empty($house)){
                $structureStyle[$key]['config_url'] = "?structure=".$val["config_id"]."&house=".$house;
            }else{
                $structureStyle[$key]['config_url'] = "?structure=".$val["config_id"];
            }
        }
        //户型结构
        $houseStyle = $config->configType("DrawingProperty");
        foreach ($houseStyle as $key=>$val){
            if($val["config_id"] == $house){
                $houseName = $val['config_name'];
            }
            if(!empty($structure)){
                $houseStyle[$key]['config_url'] = "?structure=".$structure."&house=".$val["config_id"];
            }else{
                $houseStyle[$key]['config_url'] = "?house=".$val["config_id"];
            }
        }
        //右边案例推荐
        $rightCase = $cases->leftCaseList();
        //热门标签
        $hotTag =  $information->randKeywordInfo();
        //右侧24篇文章
        $informationList =  $information->informationList($classification_id = "",$limit = "18",$left="6",$center="12");
        $seo_info = $this->selectKey($layctName,$styleName,$structureName,$houseName);
        return view('design/designlists',[
            'drawing_list' => $drawingList,
            'drawingListCount' => $drawingListCount,
            'config_info' => $config_info,
            'page' => $drawingList->render(),
            'structureStyle' => $structureStyle,
            'drawingStyle' => $drawingStyle,
            'houseStyle' => $houseStyle,
            'drawingfloor' => $drawingfloor,
            'style_id' => $style_id,
            'layct' => $layct,
            'layctName' => $layctName,
            'stylePY' => $stylePY,
            'styleName' => $styleName,
            'layctPY' => $layctPY,
            'baseUrl' => $baseUrl,
            'structure' => $structure,
            'structureName' => $structureName,
            'house' => $house,
            'houseName' => $houseName,
            'paraUrl' => $paraUrl,
            'orderParaUrl' => $orderParaUrl,
            'order' => $order,
            'hotTag' => $hotTag,
            'rightCase' => $rightCase,
            'informationList' => $informationList,
            'seo_info' => $seo_info
        ]);
    }

    //SEO标题描述户型结构匹配
    function selectKey($layctName='',$styleName='',$structureName='',$houseName=''){
        if(!empty($layctName)&&empty($styleName)&&empty($structureName)&&empty($houseName)){
            $seo_Info['seo_title'] = "农村".$layctName."房屋设计图|".$layctName."别墅设计图|".$layctName."别墅图片大全-别墅工场";
            $seo_Info['seo_keyword'] = "农村".$layctName."房屋设计图,".$layctName."别墅设计图,".$layctName."小别墅户型图,".$layctName."别墅图片大全";
            $seo_Info['seo_description'] = "农村".$layctName."房屋设计图，带外观效果图，含独栋别墅设计、联排别墅设计、双拼别墅设计等多种样式、有中式、欧式、法式、现代等多种风格，施工图、户型图、效果图齐全，保证让您找到满意的图纸";
            return $seo_Info;
        }elseif (empty($layctName)&&!empty($styleName)&&empty($structureName)&&empty($houseName)){
            $seo_Info['seo_title'] = $styleName."别墅设计图|".$styleName."设计效果图|".$styleName."别墅图片大全-别墅工场";
            $seo_Info['seo_keyword'] = $styleName."别墅设计图,农村".$styleName."别墅平面图,".$styleName."别墅小别墅户型图,".$styleName."别墅图片大全";
            $seo_Info['seo_description'] = "农村".$styleName."别墅设计图，带外观效果图，含独栋别墅设计、联排别墅设计、双拼别墅设计等多种样式、有中式、欧式、法式、现代等多种风格，施工图、户型图、效果图齐全，保证让您找到满意的图纸";
            return $seo_Info;
        }elseif (empty($layctName)&&empty($styleName)&&!empty($structureName)&&empty($houseName)){
            $seo_Info['seo_title'] = $structureName."别墅设计图|农村".$structureName."别墅平面图,".$structureName."别墅图片大全-别墅工场";
            $seo_Info['seo_keyword'] = $structureName."别墅设计图,农村".$structureName."别墅平面图,".$structureName."小别墅户型图,".$structureName."别墅图片大全";
            $seo_Info['seo_description'] = "农村".$structureName."别墅设计图，带外观效果图，含独栋别墅设计、联排别墅设计、双拼别墅设计等多种样式、有中式、欧式、法式、现代等多种风格，施工图、户型图、效果图齐全，保证让您找到满意的图纸";
            return $seo_Info;
        }elseif (empty($layctName)&&empty($styleName)&&empty($structureName)&&!empty($houseName)){
            $seo_Info['seo_title'] = "全套专业农村".$houseName."设计图纸-别墅工场";
            $seo_Info['seo_keyword'] = "农村".$houseName."设计图纸，乡村".$houseName."设计图纸";
            $seo_Info['seo_description'] = "这里有数百款农村".$houseName."设计图纸，无论你是想要一层、二层、三层的别墅图纸，或者是简欧、新中式、欧式的设计风格，在这里总有一套别墅图纸可以满足你的要求！";
            return $seo_Info;
        }elseif(empty($layctName)&&empty($styleName)&&empty($structureName)&&empty($houseName)) {
            $seo_Info = '';
            return $seo_Info;
        }else{
            if(empty($houseName)){
                $seo_Info['seo_title'] = $layctName.$styleName.$structureName."别墅设计图纸-别墅工场";
                $seo_Info['seo_keyword'] = $layctName.$styleName.$structureName.'别墅设计图';
                $seo_Info['seo_description'] = "这里有数百款".$layctName.$styleName.$structureName."别墅设计图纸，无论你是想要一层、二层、三层的别墅图纸，或者是简欧、新中式、欧式的设计风格，在这里总有一套别墅图纸可以满足你的要求！";
                return $seo_Info;
            }else{
                $seo_Info['seo_title'] = $layctName.$styleName.$structureName.$houseName."设计图纸-别墅工场";
                $seo_Info['seo_keyword'] = $layctName.$styleName.$structureName.$houseName.'设计图';
                $seo_Info['seo_description'] = "这里有数百款".$layctName.$styleName.$structureName.$houseName."设计图纸，无论你是想要一层、二层、三层的别墅图纸，或者是简欧、新中式、欧式的设计风格，在这里总有一套别墅图纸可以满足你的要求！";
                return $seo_Info;
            }
        }
    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:图纸详情
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function designDetail()
    {
        $drawing_id = input("drawing_id/d");
        $Drawing = new Drawing();
        $config = new \app\index\model\Config();;
        $information = new Information();
        $browseRecord = new Browserecord();

        //将no和no2直接存起来传聊天用
        $drawingInfo = $Drawing->dataInfo('old_img_id',$drawing_id);
        //找不到信息跳转404
        if(empty($drawingInfo)){
            abort(404,'页面不存在');
        }
        session("no",$drawingInfo["no"]."-".$drawingInfo["no2"]);
        $browseRecord->mySave(["key"=>md5(cookie('cookie_name')),"access_time"=>time(),"drawing_id"=>$drawingInfo["drawing_id"]]);//将查看的图纸作为浏览记录存起来
        $drawing_atlas = json_decode($drawingInfo['drawing_atlas']);//图集
        $config_info = $config->drawingList();//配置参数的ID
        //方案号
        $no_list = $Drawing->drawingList($drawingInfo["no"]);
        //start获取轮播波并且按照建筑效果图-渲图-施工案例图-室内设计效果图-景观设计效果图排序
        if(!empty($drawing_atlas)){
            //将平面方案图与建筑效果图单独拿出来
            $i = 0;
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 460://平面方案图
                        foreach ($val->data as $k=>$v){
                            $planarPlan[$k]['name'] = $v->name;//平面方案图
                            $planarPlan[$k]['url'] = $v->url;
                        };
                        $this->assign("planarPlan",$planarPlan);
                        break;
                    case 469://建筑效果图
                        foreach ($val->data as $k=>$v){
                            $effectPicture[$k]['name'] = $v->name;//建筑效果图
                            $effectPicture[$k]['url'] = $v->url;
                            $carousel[$i]['name'] = $v->name;//建筑效果图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        $this->assign("effectPicture",$effectPicture);
                        break;
                    case 462://正立面图
                        foreach ($val->data as $k=>$v){
                            $zhengPicture[$k]['name'] = $v->name;//正立面图
                            $zhengPicture[$k]['url'] = $v->url;
                        };
                        $this->assign("zhengPicture",$zhengPicture);
                        break;
                    case 468://背立面图
                        foreach ($val->data as $k=>$v){
                            $beiPicture[$k]['name'] = $v->name;//背立面图
                            $beiPicture[$k]['url'] = $v->url;
                        };
                        $this->assign("beiPicture",$beiPicture);
                        break;

                }
            }
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 463:
                        foreach ($val->data as $k=>$v){
                            $carousel[$i]['name'] = $v->name;//渲图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        break;
                }
            }
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 461://施工案例图
                        foreach ($val->data as $k=>$v){
                            $carousel[$i]['name'] = $v->name;//施工案例图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        break;
                }
            }
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 471://室内设计效果图
                        foreach ($val->data as $k=>$v){
                            $carousel[$i]['name'] = $v->name;//室内设计效果图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        break;
                }
            }
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 470://景观设计效果图
                        foreach ($val->data as $k=>$v){
                            $carousel[$i]['name'] = $v->name;//景观设计效果图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        break;
                }
            }
            foreach ($drawing_atlas as $key=>$val){
                switch ($key){
                    case 463://渲图
                        foreach ($val->data as $k=>$v){
                            $carousel[$i]['name'] = $v->name;//渲图
                            $carousel[$i]['url'] = $v->url;
                            $i++;
                        };
                        break;
                }
            }
            if(!empty($carousel)){
                $this->assign("carousel",$carousel);
            }
        }
        //end获取轮播波并且按照建筑效果图-渲图-施工案例图-室内设计效果图-景观设计效果图排序
        //感兴趣的图纸
        $drawingList = $Drawing->interestDrawing();
        //下方8篇新闻
        $informationList = $information->informationList($classification_id="",$limit="8");

        return view('design/designDetail',[
            'drawingInfo' => $drawingInfo,
            'config_info' => $config_info,
            'informationList' => $informationList,
            'drawingList' => $drawingList,
            'no_list' => $no_list
        ]);
    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:提交订单
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function postOrder()
    {
        $drawing_id = input('drawing_id/d');
        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $ImgApiData = config("ImgApiData");
        $shengList = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["addressList"], ["pid"=>0]);
        $shengList = json_decode($shengList,true);
        return view('design/postOrder',[
            'shengList' => $shengList["data"],
            'drawing_id' => $drawing_id
        ]);
    }

    /**
     * Author:yang you
     * Time:2019-01-15 10:00:43
     * Describe:购买图纸
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyImg()
    {
        $name = input('name/s');
        $tel = input('tel/s');
        $sheng = input('sheng/d');
        $shi = input('shi/d');
        $xian = input('xian/d');
        $address = input('address/s');
        $remarks = input('remarks/s');
        $drawing_id = input('drawing_id/d');
        $yanma = input("yanma");
        $yanma2 = session($tel.'yzm');
        if (trim($tel) == "") {
            echo json_encode("请填写您的手机号码");
            exit();
        }
        if (trim($yanma) == "") {
            echo json_encode("请填写您收到的验证码");
            exit();
        }
        if ($yanma2 == null) {
            echo json_encode("您输入的验证码已过期或不存在请重新获取");
            exit();
        }
        if($yanma!=$yanma2){
            echo json_encode("验证码错误，请核对后再试");
            exit();
        }
        $user = new User();
        $UserCookie = new UserCookie();
        $isphone = $user->checkName($tel);//检查是否为新用户
        if ($isphone != null) {
            if ($isphone['user_id']) {
                $cookieInfo = $UserCookie->cookieName(md5(cookie('cookie_name')));
                //如果该cookie没有绑定user_id就将其绑定
                if(empty($cookieInfo["user_id"])){
                    $UserCookie->mySave(["bind_time"=>time(),"user_id"=>$isphone["user_id"]],"key",md5(cookie('cookie_name')));
                }
                $usinfo = array("user_id" => $isphone['user_id'], "tel" => $isphone['tel'], "nick_name" => $isphone['nick_name']);
                session('user_info',$usinfo);
            } else {
                echo json_encode("登陆出错请稍后再试");
            }
        }else{
            //号码不存在创建新用户
            $data = ['tel'=>$tel,'create_ip'=>request()->ip(),'nick_name'=>$tel];
            $username = $user->mySave($data,"user_id",0);
            //将user_id与user_cookie绑定
            $isphone = $user->checkName($tel);
            $UserCookie->mySave(["bind_time"=>time(),"user_id"=>$isphone["user_id"]],"key",md5(cookie('cookie_name')));
            if ($username) {
                session('user_info',$data);
            } else {
                echo json_encode("登陆出错请稍后再试");
            }
        }
        $Order = new Order();
//        $Drawing = new Drawing();
//        $drawPrice = $Drawing->getFieldValue('drawing_id',$drawing_id,'price');
//        $result = $Order->mySave(["phone"=>$tel,"title"=>$name,"sheng"=>$sheng,"shi"=>$shi,"xian"=>$xian,"address"=>$address,"remarks"=>$remarks,"drawing_id"=>$drawing_id,"price"=>$drawPrice,"uid"=>$isphone["user_id"]]);
        $result = $Order->mySave(["phone"=>$tel,"title"=>$name,"sheng"=>$sheng,"shi"=>$shi,"xian"=>$xian,"address"=>$address,"remarks"=>$remarks,"drawing_id"=>$drawing_id,"uid"=>$isphone["user_id"]]);
        echo $result;
    }

}